/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import model.Orders;

/**
 *
 * @author William
 */
public class OrdersDAO {

    public OrdersDAO() {
    }

    public void create(Orders orders, EntityManager em) {
        try {
            em.persist(orders);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public Orders search(Long id, EntityManager em) {
        Orders orders = null;
        try {
            orders = em.find(Orders.class, id);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return orders;
    }

    public void update(Orders orders, EntityManager em) {
        try {
            em.merge(orders);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public void delete(Orders orders, EntityManager em) {
        try {
            em.remove(orders);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public List<Orders> listAll(EntityManager em) {
        List<Orders> ordersList = new ArrayList<>();
        Query q = em.createQuery("SELECT u FROM Orders u");
        try {
            List<Orders> orderss = new ArrayList<Orders>();
            orderss = q.getResultList();
            for (Object s : orderss) {
                ordersList.add((Orders) s);
            }
            return ordersList;
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return ordersList;
    }
}
